import { ToastAndroid } from "react-native";

class Socket {
    socket = null;

    create () {
        if (this.socket != null) {
            return;
        }

        this.socket = new WebSocket("ws://10.127.202.103:8001");

        this.socket.onopen = () => {
            ToastAndroid.show("connect", ToastAndroid.SHORT);

            this.socket.send("hello server!");
        };

        this.socket.onmessage = data => {
            ToastAndroid.show("Received " + data, ToastAndroid.SHORT);
        };

        this.socket.onerror = () => {
            ToastAndroid.show("error", ToastAndroid.SHORT);
        };

        this.socket.onclose = () => {
            ToastAndroid.show("disconnect", ToastAndroid.SHORT);
        };
    }

    send (data) {
        this.socket.send(data.toString());
    }
}

const socket = new Socket();

export default socket;